package cn.bdqn.service.impl;

import cn.bdqn.domain.Course;
import cn.bdqn.domain.Dict;
import cn.bdqn.domain.Teacher;
import cn.bdqn.mapper.CourseMapper;
import cn.bdqn.mapper.DictMapper;
import cn.bdqn.mapper.TeacherMapper;
import cn.bdqn.service.CourseService;
import cn.bdqn.utils.result.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 课程实现类
 * @author Wang-Hao
 */
@Service("CourseService")
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private TeacherMapper teacherMapper;


    /**
     * 查询全部课程信息，分页显示
     * @param pageNum   当前页码
     * @param pageAmount    每页条数
     * @return
     */
    @Override
    public Page queryCourseAllInfoPaging(Integer pageNum, Integer pageAmount) {

        //开始分页
        PageHelper.startPage(pageNum>0?pageNum:1,pageAmount);
        //获取全部课程信息
        List<Course> list = courseMapper.selectCourseAllInfo();
        //分页
        PageInfo<Course> coursePageInfo = new PageInfo<Course>(list);
        //封装分页的信息
        Page page = new Page();
        page.setMenuList(coursePageInfo.getList());
        page.setTotalRecord((int)coursePageInfo.getTotal());

        return page;
    }

    /**
     * 根据课程id查询课程信息
     * @param courseId 课程id
     * @return
     */
    @Override
    public Course queryCourseByCourseId(Integer courseId) {
        //调用mapper层根据课程id查询课程信息的方法
        return courseMapper.selectByPrimaryKey(courseId);
    }

    /**
     * 添加课程信息
     * @param course 课程对象
     */
    @Override
    public void insertCourseInfo(Course course) {
        //根据教师id查询教师
        Teacher teacher = teacherMapper.selectById(course.getTeacherId());
        //根据字典id查询
        Dict dict = dictMapper.selectByPrimaryKey(course.getCourseFeatureId());
        //赋值课题名
        course.setCourseFeatureName(dict.getDicName());
        course.setTeacher(teacher);
        course.setIssueTime(new Date());
        //将课程状态设置成未发布
        course.setIssueState(1);
        //调用mapper层添加的方法
        courseMapper.insert(course);
    }

    /**
     * 根据地址、适合学段、课程特色查询课程信息，分页显示
     * @param pageNum 页码
     * @param pageAmount 每页显示的数量
     * @param baseAddress 地址
     * @param fitAge   适合学段
     * @param courseFeature 课程特色
     * @return
     */
    @Override
    public Page queryCourseInfoPagingByConditions(Integer pageNum, Integer pageAmount, String baseAddress, String fitAge, Integer courseFeature) {

        //开始分页
        PageHelper.startPage(pageNum>0?pageNum:1,pageAmount);
        //获取符合条件的信息
        List<Course> list = courseMapper.selectCourseInfoPagingByConditions(baseAddress,fitAge,courseFeature);
        //分页
        PageInfo<Course> coursePageInfo = new PageInfo<Course>(list);
        //封装信息
        Page page = new Page();
        page.setMenuList(coursePageInfo.getList());
        page.setTotalRecord((int)coursePageInfo.getTotal());
        return page;
    }

    /**
     * 根据课程id修改课程状态
     * @param courseId
     */
    @Override
    public void updateIssueState(Integer courseId,Integer issueState) {
        //调用mapper层的修改状态的方法
        courseMapper.updateByIssueState(courseId,issueState);
    }

    /**
     * 根据课程id修改课程信息
     * @param course
     */
    @Override
    public void updateCourseData(Course course) {
        System.out.println(course.getTeacherId());
        //根据教师id查询
        Teacher teacher = teacherMapper.selectById(course.getTeacherId());
        //赋值
        course.setTeacher(teacher);
        //根据字典id查询
        Dict dict = dictMapper.selectByPrimaryKey(course.getCourseFeatureId());
        //赋值课题名
        course.setCourseFeatureName(dict.getDicName());
        course.setIssueTime(new Date());
        //调用mapper层根据id修改课程信息的方法
        courseMapper.updateByPrimaryKeySelective(course);
    }

    /**
     * 根据基地id查询课程信息
     * @param baseId 基地id
     * @return
     */
    @Override
    public Page queryCourseInfoByBaseId(Integer baseId, Integer pageNum, Integer pageAmount) {
        //开始分页
        PageHelper.startPage(pageNum>0?pageNum:1,pageAmount);
        //获取符合条件的信息
        List<Course> list = courseMapper.selectInfoByBaseId(baseId);
        //分页
        PageInfo<Course> coursePageInfo = new PageInfo<Course>(list);
        //封装信息
        Page page = new Page();
        page.setMenuList(coursePageInfo.getList());
        page.setTotalRecord((int)coursePageInfo.getTotal());
        return page;
    }
}
